package org.cnbi.dao.impl;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.cnbi.dao.IHandleDao;
import org.cnbi.entity.SysLog;
import org.cnbi.utils.date.DateUtil;
import org.cnbi.utils.pagination.PagingUtil;
import org.cnbi.utils.pojo.Pagination;
import org.springframework.stereotype.Repository;
@Repository
public class IHandleDaoImpl extends org.cnbi.dao.base.BaseDao implements IHandleDao {
	/**
	 * select top 10 (select * from user) start  limit ,,,,,,
	 */
	@Override
	public Pagination queryPagination(PagingUtil page, Object object) {
		Pagination paging = new Pagination(page, object);
		String listSql = getPaginationSql(page.getSql(), paging.getCurrentPage(), paging.getNumPerPage());
		List<?> list = super.query(listSql, object);
		paging.setTotalRows(getTotalRow(page,object));
		paging.setTotalPages();
		// 装入结果集
		paging.setResultList(list);

		return paging;
	}
	
	
	/**
	 * 返回当前  表的所有记录条数
	 * **/
	private int getTotalRow(PagingUtil page,Object obj) {
		String sql = page.getSql();
		sql = sql.substring(0, sql.indexOf(" order by "));
		// 计算总记录数
		StringBuffer totalSQL = new StringBuffer(" SELECT count(*) FROM ( ");
		totalSQL.append(sql);
		totalSQL.append(" ) totalTable ");
		return super.queryCount(totalSQL.toString(),obj);
	}



	@Override
	public boolean queryUserLicenseLastTimeWhetherPastdue(String sql) {
		List<SysLog> list = (List<SysLog>) super.query(sql, new SysLog());
		for (SysLog log : list) {
			Date date = DateUtil.convertDate(log.getOperateTime(), DateUtil.defaultFormat);
			long lastLoginTime =date.getTime();
			long now = new Date().getTime();
			if (lastLoginTime > now){
				return true;
			}
		}
		return false;
	}

	@Override
	public List<Map> queryListMapBean(String sql, Object object){
		return super.queryListMapBean(sql, object);
	}


	@Override
	public int[] batchUpdate(String sql, List<Object[]> list) {
		
		return super.batchUpdate(sql,list);
	}

}
